from flask_appbuilder import IndexView, expose, has_access
#from flask_login import current_user
from werkzeug.utils import redirect
from flask import request


class MyIndexView(IndexView):
    index_template = 'index.html'

    @expose('/')
    def index(self):
        from flask_login import current_user
        # if not current_user.is_anonymous():
        #     return redirect('/running')
        self.update_redirect()
        return self.render_template(self.index_template,
                                    appbuilder=self.appbuilder,
                                    is_admin = self.is_admin())

    @expose("/upload", methods=['POST'])
    def upload(self):

        return 'ok'


    def is_admin(self):
        from flask_login import current_user
        if current_user and hasattr(current_user, 'roles'):
            for role in current_user.roles:
                if role.name == 'Admin':
                    return True
        return False